package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.pojo.LikeAdd;
import com.itheima.service.UserLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author wangjiexin
 */
@RestController
@RequestMapping("/user/like")
public class UserLikeController {
    @Autowired
    UserLikeService userLikeService;
    //添加喜欢
    @PostMapping
    public CommonResult add(@RequestBody LikeAdd dto){
        //把喜欢的用户id传进来
       boolean res = userLikeService.add(dto);
       if (!res){
           throw new RuntimeException("喜欢添加失败");
       }
        return CommonResult.success(res);
    }
    //取消喜欢
    @DeleteMapping
    public CommonResult delLike(@RequestParam String id){
        boolean res = userLikeService.del(id);
      /*  if (!res){
            throw new RuntimeException("取消喜欢失败");
        }*/
      // 这边也可以用三元运算
        return res? CommonResult.success("取消喜欢成功"):CommonResult.error("取消喜欢失败");
    }

    @GetMapping
    public CommonResult getLikeList(@RequestParam(required = false) String name,
                                    @RequestParam Integer type,
                                    @RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize){

        return userLikeService.list(name, type, page, pageSize);
    }
}
